package com.anbao.newanbao;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.anbao.action.ActivityManagerAction;
import com.anbao.service.BaseMessgeService;
import com.anbao.service.MessService;

public class MainActivity extends Activity implements OnClickListener {

	private Button waichubufangBtn;
	private Button cefangBtn;
	private Button areaManageBtn;
	private Button cleanAlarmBtn;
	private Button queryAlarmHistoryBtn;
	private Button settingBtn;

	/**
	 * 布防code值
	 */
	private final int BUFANGCODE = 1;
	/**
	 * 撤防code值
	 */
	private final int CHEFANGCODE = 2;
	/**
	 * 撤销告警code值
	 */
	private final int CHEXIAOGAOJINGCODE =3;
	
	
	private MainActivityServiceImpl mainService = new MainActivityServiceImpl();
	
	private ImageView deviceImg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		//将当前界面加入到系统管理中国
		ActivityManagerAction.getScreenManager().pushActivity(this);
		iniView();
	}

	private void iniView() {
		waichubufangBtn = (Button) findViewById(R.id.bufang_btn);
		waichubufangBtn.setOnClickListener(this);
		cefangBtn = (Button) findViewById(R.id.cefang_btn);
		cefangBtn.setOnClickListener(this);
		areaManageBtn = (Button) findViewById(R.id.areaManagebtn);
		areaManageBtn.setOnClickListener(this);
		cleanAlarmBtn = (Button) findViewById(R.id.cexiaobaojing_btn);
		cleanAlarmBtn.setOnClickListener(this);
		queryAlarmHistoryBtn = (Button) findViewById(R.id.jiluchaxun_btn);
		queryAlarmHistoryBtn.setOnClickListener(this);
		settingBtn = (Button) findViewById(R.id.stting_btn);
		settingBtn.setOnClickListener(this);
		deviceImg = (ImageView) findViewById(R.id.bacImg);

		setdeviceImgLocation();

	}

	/**
	 * 设置设备的位置
	 */
	private void setdeviceImgLocation() {

		// 获取屏幕密度（方法2）
		DisplayMetrics dm = new DisplayMetrics();
		dm = getResources().getDisplayMetrics();

		float density = dm.density; // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）

		int screenWidth = dm.widthPixels; // 屏幕宽（像素，如：480px）
		int screenHeight = dm.heightPixels; // 屏幕高（像素，如：800px）

		Log.e("打印", density + " " + screenWidth + " " + screenHeight);
		LinearLayout.LayoutParams layoutParams = (LayoutParams) deviceImg
				.getLayoutParams();
		layoutParams.width = screenWidth;
		layoutParams.height = (int) (screenHeight - 350 * density);
		deviceImg.setLayoutParams(layoutParams);

		showMessageDialog();
		showTitleMessage();
	}

	private void showTitleMessage() {
		//消息通知栏
        //定义NotificationManager
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        //定义通知栏展现的内容信息
        int icon = R.drawable.device;
        CharSequence tickerText = "我的通知栏标题";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
         
        //定义下拉通知栏时要展现的内容信息
        Context context = getApplicationContext();
        CharSequence contentTitle = "告警消息";
        CharSequence contentText = "告警设备1产生告警";
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
         
        //用mNotificationManager的notify方法通知用户生成标题栏消息通知
        mNotificationManager.notify(1, notification);
	}

	private void showMessageDialog() {

		new AlertDialog.Builder(MainActivity.this)
				.setTitle("告警消息")
				.setMessage("这是一条告警消息")
				.setPositiveButton("解除报警",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								
								chexiaobaojing();
								
							}
						}).setNegativeButton("暂不处理", null).show();

	}

	protected void chexiaobaojing() {

		
		Intent intent = new Intent(MainActivity.this,InputPwdActivity.class);
		startActivityForResult(intent, CHEXIAOGAOJINGCODE);
		
	}

	@Override
	public void onClick(View arg0) {
		Intent intent = null;
		switch (arg0.getId()) {
		case R.id.bufang_btn:

			bufang();

			break;
		case R.id.cefang_btn:
			chefang();
			break;
		case R.id.areaManagebtn:

			break;
		case R.id.cexiaobaojing_btn:

			chexiaobaojing();
			
			break;
		case R.id.jiluchaxun_btn:
			intent = new Intent(MainActivity.this, QueryAlarmActivity.class);
			startActivity(intent);
			break;
		case R.id.stting_btn:
			intent = new Intent(MainActivity.this, SettingActivity.class);
			startActivity(intent);
			break;

		}

	}

	private void chefang() {
		Intent intent = new Intent(MainActivity.this,InputPwdActivity.class);
		startActivityForResult(intent, CHEFANGCODE);
		
	}

	private void bufang() {

			Intent intent = new Intent(MainActivity.this,InputPwdActivity.class);
			startActivityForResult(intent, BUFANGCODE);
			
	}
	
	
    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

       // TODO Auto-generated method stub

       super.onActivityResult(requestCode, resultCode, data);
     
       caozuo(requestCode, resultCode);
     
    }

	private void caozuo(int requestCode, int resultCode) {
		  if (requestCode == BUFANGCODE)
	       {

	           if (resultCode == RESULT_OK)

	           {
	        	   
	        	   mainService.bufang();
	        	   
	        	 
	           }

	       }else if(requestCode == CHEFANGCODE){
	    	   if (resultCode == RESULT_OK)

	           {
	    		   
	    		   mainService.chefang();
	    		  
	           }
	    	   
	       }else if(requestCode == CHEXIAOGAOJINGCODE){
	    	   
	    	   if (resultCode == RESULT_OK)

	           {
	    		   
	    		   mainService.chexiaogaojing();
	    		   
	           }
	       }		
	}
	
	
 	class MainActivityServiceImpl  extends BaseMessgeService implements MessService{

		

		/**
		 * 布放
		 * 
		 */
		public  void bufang() {
			
			
              
			 
			
		}

		/**
		 * 撤防
		 * 
		 */
		public   void chefang() {
			 Toast.makeText(MainActivity.this, "执行撤防动作", 1).show();
		}

		/**
		 * 撤销告警
		 * 
		 */
		public   void chexiaogaojing() {
			Toast.makeText(MainActivity.this, "执行撤销报警动作", 1).show();
			
			
			
			
		}

		@Override
		public boolean sendMessage(byte[] data) {
			return false;
		}

		@Override
		public void regMessageResult() {
		
		}

	}


}
